/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.client;

import entity.Candidat;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.restlet.resource.ClientResource;
import org.restlet.resource.ResourceException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import service.CandidatFacadeREST;

/**
 *
 * @author Paul
 */
public class ServletCandidat extends HttpServlet {

    @EJB
    private CandidatFacadeREST candidatFacadeREST;
    public static final String VUE_LISTE_CANDIDATS = "/WEB-INF/listeCandidats.jsp";
    public static final String URL_WS = "http://localhost:8080/PortailCentreFormation/webresources/entity.candidat";

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(true);
        List<Candidat> listeCandidats = candidatFacadeREST.findAll();
        session.setAttribute("listeCandidats", listeCandidats);
        afficherListeCandidats(request, response);
        this.getServletContext().getRequestDispatcher(VUE_LISTE_CANDIDATS).forward(request, response);
    }

    private List<Candidat> afficherListeCandidats(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter out = response.getWriter();
        response.setContentType("text/html; charset=utf-8");
        ClientResource resource = null;
        Candidat c;
        List<Candidat> listeCandidats = new ArrayList<Candidat>();
        try {// Preparer l'appel au service Web distant
            request.getRequestURL();
            // + "?" + request.getQueryString()
            resource = new ClientResource(URL_WS);
            // Recuperer la reponse en arbre DOM
            Document xml = resource.get(Document.class);
            System.out.println(xml.getDocumentElement());
            NodeList candidats = xml.getElementsByTagName("candidats");
            for (int i = 0; i < candidats.getLength(); i++) {
                NodeList candidat = xml.getElementsByTagName("candidat");
                for (int j = 0; j < candidat.getLength(); j++) {
                    Element elt = (Element) candidat.item(j);
                    NodeList childs = elt.getChildNodes();
                    c = new Candidat();
                    for (int k = 0; k < childs.getLength(); k++) {
                        String nomBalise = childs.item(k).getNodeName();
                        System.out.println("Nom de la balise : " + nomBalise);
                        String textContent = childs.item(k).getTextContent();
                        System.out.println("Contenu de la balise : " + textContent);
                        //liste.put(childs.item(k).getNodeName(), childs.item(k).getTextContent());
                        if ("competences".equals(nomBalise)) {
                            c.setCompetences(textContent);
                        } else if ("diplomes".equals(nomBalise)) {
                            c.setDiplomes(textContent);
                        } else if ("motivation".equals(nomBalise)) {
                            c.setMotivation(textContent);
                        } else if ("situationPro".equals(nomBalise)) {
                            c.setSituationPro(textContent);
                        }
                    }
                    listeCandidats.add(c);
                }
            }
        } catch (ResourceException exc) {
            out.println("Erreur : " + exc.getStatus().getCode() + " ("
                    + exc.getStatus().getDescription() + ") : "
                    + resource.getResponseEntity().getText());
        }
        return listeCandidats;
    }
}
